package com.bw.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.common.ResponseResult;
import com.bw.goods.entity.EGoods;
import com.bw.goods.service.IEGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xingjinning
 * @since 2020-07-05
 */
@RestController
@RequestMapping("/goods")
public class EGoodsController {

    @Autowired
    private IEGoodsService goodsService;
    @PostMapping("/list/{pagenum}/{pagesize}")
    public ResponseResult list(@PathVariable("pagenum")int pagenum, @PathVariable("pagesize")int pagesize, @RequestBody EGoods goods){
        ResponseResult result = ResponseResult.SUCCESS();
        QueryWrapper queryWrapper = new QueryWrapper();
        PageHelper.startPage(pagenum,pagesize);
        if(goods.getGname()!=null){
            queryWrapper.like("gname",goods.getGname());
        }
        queryWrapper.orderByDesc("ctime");
        List list = goodsService.list(queryWrapper);
        PageInfo pageInfo = new PageInfo(list);
        result.setResult(pageInfo);
        return result;
    }

    @GetMapping("/getGoods/{gid}")
    public ResponseResult getGoods(@PathVariable("gid")long gid){
        ResponseResult result = ResponseResult.SUCCESS();
        QueryWrapper queryWrapper = new QueryWrapper();


        queryWrapper.eq("id",gid);
        EGoods eGoods = goodsService.getOne(queryWrapper);
        result.setResult(eGoods);
        return result;
    }
    @GetMapping("getname")
    public ResponseResult getname(){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            result.setResult(goodsService.list());
        }catch (Exception e){
            result=ResponseResult.FAIL();
        }
        return result;
    }
}
